package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.db.utils.RedisTokenUtil;
import com.yt.user.models.bo.LoginInfoBO;
import com.yt.user.models.bo.LoginInfoSaveBO;
import com.yt.user.models.dto.LoginInfoDTO;
import com.yt.user.models.service.ILoginInfoService;
import com.yt.user.models.vo.EmployeeInfoVO;
import com.yt.user.models.vo.LoginInfoVO;
import com.yt.user.models.vo.StudentInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author
 * @dese
 * @date 2025/10/23 10:19
 */
@RestController
@RequestMapping("/api/loginInfo")
@Slf4j
@Api(value = "LoginInfoApiController", tags = "登录信息Api接口")
public class LoginInfoApiController {

    @DubboReference
    private ILoginInfoService loginInfoService;
    @Autowired
    private RedisTokenUtil redisTokenUtil;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<LoginInfoDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        LoginInfoDTO dto = loginInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "登录信息列表", notes = "登录信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<LoginInfoDTO>> queryPageList(LoginInfoBO bo) {
        Wrapper<LoginInfoDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<LoginInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = loginInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public Result<HashMap<String, Object>> login(@RequestBody LoginInfoSaveBO saveBO) {
        HashMap<String, Object> map = new HashMap<>();
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }


        //1.判断账号是否存在
        LoginInfoVO vo = loginInfoService.getLogin(saveBO);
        if (vo == null) {
            return new Result<>(CodeEnum.FAIL_USER_200003);
        }
        //2.账号是系统用户表（员工）or 客户表;
        String role = loginInfoService.judgment(saveBO);

        //3.判断账号是员工还是客户
        if ("学生".equals(role)) {
            //3.1.1判断客户密码是否正确
            StudentInfoVO studentInfoVO = loginInfoService.getStudentLogin(saveBO);
            if (studentInfoVO == null) {
                return new Result<>(CodeEnum.FAIL_USER_200006);
            }
            //3.1.2创建token
            String token = redisTokenUtil.generateToken(studentInfoVO.getName());
            studentInfoVO.setToken(token);
            //角色
            studentInfoVO.setRoleName(role);
            //3.1.3put到map里
            map.put("studentInfo", studentInfoVO);
        } else {
            //3.2.1判断员工密码是否正确
            EmployeeInfoVO employeeInfoVO = loginInfoService.getEmployeeLogin(saveBO);
            if (employeeInfoVO == null) {
                return new Result<>(CodeEnum.FAIL_USER_200006);
            }
            //3.2.2创建token
            String token = redisTokenUtil.generateToken(employeeInfoVO.getUserName());
            employeeInfoVO.setToken(token);
            //角色
            employeeInfoVO.setRoleName(role);
            //3.2.3put到map
            map.put("employeeInfo", employeeInfoVO);
        }
        return new Result<>(map);
    }

}

